/**
 *	@author		Juntalis
 */

/** 
* Miscellaneous utility functions
*/
var Misc = {

	/**
	 * Checks if value is numerical
	 * @param {String} sText
	 * @return {Boolean}
	 */
	isNumeric : function (sText,blFloat) {
		if(sText==null) return false;
		if(typeof(sText) == "Number") return true;
		if(!blFloat) blFloat = false;
		var ValidChars;
		if(blFloat)
			ValidChars = "0123456789.";
		else
			ValidChars = "0123456789";
		
		var IsNumber = true; var Char;
		for (i = 0; i < sText.length && IsNumber == true; i++) {
			Char = sText.charAt(i);
			if (ValidChars.indexOf(Char) == -1) IsNumber = false;
		}
		return IsNumber;
	},

	/**
	 * Returns a unique string for use in an ID.
	 * @param {Integer}	[length]	Length of the string.
	 * @return {String}	
	 */
	UniqueID : function (length) {
		var iTest = 1;
		var str = '';
		while (iTest) {
			var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split('');
			var sTest = length;
			if ( (!length) || (!this.isNumeric(sTest)) ) {
				length = Math.floor(Math.random() * chars.length);
			}
			
			str = '';
			for (var i = 0; i < length; i++) {
				str += chars[Math.floor(Math.random() * chars.length)];
			}
			iTest = ($("#RndId_" + str).length);
		}
		return str;
	},
	
	StartTimer : function(callb,interv) {setTimeout(callb,interv);},
	
	/**
	 * Wait a specific amount of time before returning.I highly recommend not using this, and instead using the "StartTimer" function, due to the fact that this stops all code processing until it hits the time you asked it to wait.
	 * @param {Int} msecs Miliseconds to wait.
	 */
	Wait : function(msecs)
	{
		var start = new Date().getTime();
		var cur = start
		while(cur - start < msecs){
			cur = new Date().getTime();
		}
	},
	
	/**
	 * Launches user's default browser with specified url.
	 * @param {String} sUrl URL to launch.
	 */
	OpenUrl : function(sUrl)
	{
		var reqJunt = new air.URLRequest(sUrl);
		air.navigateToURL(reqJunt);
	}
}